package com.tanovo.service.impl;

import com.tanovo.bo.BindClientBo;
import com.tanovo.bo.CompanyQueryBo;
import com.tanovo.bo.UserBindAppBo;
import com.tanovo.config.UserInfoContext;
import com.tanovo.domain.*;
import com.tanovo.exception.ServiceException;
import com.tanovo.mapper.CompanyMapUserMapper;
import com.tanovo.mapper.CompanyMapper;
import com.tanovo.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
public class CompanyServiceImpl implements ICompanyService {

    @Autowired
    private CompanyMapUserMapper companyMapUserMapper;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private IUserService iUserService;
    @Autowired
    private IOauthClientDetailsService iOauthClientDetailsService;

    @Override
    public void bindUser(User user, Integer companyId) {
        CompanyMapUser companyMapUser = new CompanyMapUser();
        companyMapUser.setCompanyId(companyId);
        companyMapUser.setUserId(user.getId());
        companyMapUser.setEnable(1);
        companyMapUser.setDelFlag(0);
        companyMapUser.setCreateTime(new Date());
        companyMapUser.setUpdateTime(new Date());
        companyMapUserMapper.insertSelective(companyMapUser);
    }

    @Override
    public void addPartnerCompany(Company company) {
        //保存公司入库
        this.add(company);
        iUserService.bindSuperAdminToCompany(company);
        OauthClientDetails clientDetails = new OauthClientDetails();
        clientDetails.setCompanyId(1);
        List<OauthClientDetails> cientList = iOauthClientDetailsService.getCompanyCientList(clientDetails);
        //为超管开通已有应用权限
        for (OauthClientDetails client : cientList) {
            UserBindAppBo userBindAppBo = new UserBindAppBo();
            userBindAppBo.setCompanyId(company.getId());
            userBindAppBo.setUserId(1);
            userBindAppBo.setClientId(client.getClientId());
            iUserService.bindApp(userBindAppBo);
            //为应用初始化管理员
//            client.setCompanyId(company.getId());
//            iOauthClientDetailsService.initClientAdminRole(client);
        }


    }

    @Override
    public void add(Company company) {
        company.setDelFlag(0);
        company.setCreateTime(new Date());
        company.setUpdateTime(new Date());
        companyMapper.insertSelective(company);
    }

    @Override
    public void delCompanyList(List<Integer> companyIdList) {
        CompanyExample companyExample = new CompanyExample();
        companyExample.createCriteria().andIdIn(companyIdList);
        Company company = new Company();
        company.setDelFlag(1);
        company.setUpdateBy(UserInfoContext.getUser().getId().toString());
        company.setUpdateTime(new Date());
        companyMapper.updateByExampleSelective(company, companyExample);
    }

    @Override
    public void editCompany(Company company) {
        company.setUpdateBy(company.getUpdateBy());
        company.setUpdateTime(new Date());
        companyMapper.updateByPrimaryKeySelective(company);
    }

    @Override
    public List<Company> list(CompanyQueryBo companyQueryBo) {
        CompanyExample companyExample = new CompanyExample();
        CompanyExample.Criteria criteria = companyExample.createCriteria();
        if (StringUtils.isNotBlank(companyQueryBo.getName())) {
            criteria.andNameLike("%" + companyQueryBo.getName() + "%");
        }
        criteria.andDelFlagEqualTo(0);
        List<Company> companyList = companyMapper.selectByExample(companyExample);
        return companyList;
    }

    @Override
    public Company detail(Integer companyId) {
        CompanyExample companyExample = new CompanyExample();
        companyExample.createCriteria().andDelFlagEqualTo(0).andIdEqualTo(companyId);
        List<Company> companyList = companyMapper.selectByExample(companyExample);
        if (CollectionUtils.isEmpty(companyList)) {
            return null;
        }
        return companyList.get(0);
    }

    @Override
    public List<Company> userLoginCompanyList(Integer userId) {
        List<Company> companyList = companyMapper.userLoginCompanyList(userId);
        return companyList;
    }


    @Override
    public List<CompanyMapUser> getCompanyMapUserList(Integer userId) {
        CompanyMapUserExample example = new CompanyMapUserExample();
        example.createCriteria().andUserIdEqualTo(userId).andDelFlagEqualTo(0);
        example.setOrderByClause("login_default DESC");
        List<CompanyMapUser> list = companyMapUserMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list;
    }

    @Override
    public void updateCompanyMapUserList(List<CompanyMapUser> companyList) {
        for (CompanyMapUser c : companyList) {
            c.setUpdateTime(new Date());
            companyMapUserMapper.updateByPrimaryKeySelective(c);
        }

    }

    @Override
    public CompanyMapUser getLoginDefaultCompany(Integer userId) {
        CompanyMapUser companyMapUser = companyMapUserMapper.getLoginDefaultCompany(userId);
        return companyMapUser;
    }

    @Override
    public Company getCompanyByShareCode(String shareCode) {
        CompanyExample companyExample = new CompanyExample();
        companyExample.createCriteria().andDelFlagEqualTo(0).andShareCodeEqualTo(shareCode);
        List<Company> companyList = companyMapper.selectByExample(companyExample);
        if (CollectionUtils.isEmpty(companyList)) {
            return null;
        }
        return companyList.get(0);
    }

    @Override
    public void companyBindUser(Integer companyId, Integer userId) {
        List<CompanyMapUser> companyMapUserList = this.getCompanyMapUserList(userId);
        if(!CollectionUtils.isEmpty(companyMapUserList)){
            for (CompanyMapUser cu : companyMapUserList) {
                if (cu.getCompanyId().equals(companyId)) {
                    throw new ServiceException("你已经加入该公司了");
                }
            }
        }
        User user = new User();
        user.setId(userId);
        this.bindUser(user, companyId);
    }
}
